package com.forestar.xzk.commonlibrary;

import android.content.Context;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BaseUtil
  {

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dipTopx(Context mContext, float dpValue)
      {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
      }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int pxTodip(Context mContext, float pxValue)
      {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
      }

    /**
     * 正整数
     *
     * @param str
     * @return
     */
    public static boolean intMatch(String str)
      {
        String regex = "^[1-9]+\\d*$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(str);
        return m.matches();
      }

    /**
     * 特殊字符
     *
     * @param str
     * @return
     */
    public static boolean specialCharMatch(String str)
      {
        //        String regex = "[^0-9a-zA-Z\u4e00-\u9fa5]+";
        String regex = "^[A-Za-z0-9\\u4e00-\\u9fa5]+$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(str);
        return m.matches();
      }

    /**
     * 两位小数是否符合
     *
     * @param mobile
     * @return
     */
    public static boolean priceMatch(String mobile)
      {
        //        String regex = "^[0-9]+\\.[0-9]{2}$";
        String regex = "^[1-9]+\\d*(\\.\\d{0,2})?$|^0?\\.\\d{0,2}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        return m.matches();
      }

    /**
     * 判断手机号是否符合
     *
     * @param mobile
     * @return
     */
    public static boolean mobileMatch(String mobile)
      {
        String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        return m.matches();
      }

    /**
     * 判断邮箱
     *
     * @param mobile
     * @return
     */
    public static boolean mailMatch(String mobile)
      {
        String regex = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        return m.matches();
      }

    /**
     * 判断密码
     *
     * @param mobile
     * @return
     */
    public static boolean verifyPassword(String mobile)
      {
        //        String regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[\\s\\S]{8,20}$";
        String regex = "((^(?=.*[a-z])(?=.*[A-Z])(?=.*\\W)[\\da-zA-Z\\W]{8,20}$)|(^(?=.*\\d)(?=.*[A-Z])(?=.*\\W)[\\da-zA-Z\\W]{8,20}$)|(^(?=.*\\d)(?=.*[a-z])(?=.*\\W)[\\da-zA-Z\\W]{8,20}$)|(^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[\\da-zA-Z\\W]{8,20}$))";
        Log.d("verifyPassword", mobile.matches(regex)+"");
        return mobile.matches(regex);
      }

    /**
     * 判断身份证号
     *
     * @param idnumber
     * @return
     */
    public static boolean idCardMatch(String idnumber)
      {
        if (idnumber == null || "".equals(idnumber))
          {
            return false;
          }
        // 定义判别用户身份证号的正则表达式（15位或者18位，最后一位可以为字母）
        String regularExpression =
            "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|"
                + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        boolean matches = idnumber.matches(regularExpression);
        // 判断第18位校验值
        if (matches)
          {
            if (idnumber.length() == 18)
              {
                try
                  {
                    char[] charArray = idnumber.toCharArray();
                    // 前十七位加权因子
                    int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
                    // 这是除以11后，可能产生的11位余数对应的验证码
                    String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
                    int sum = 0;
                    for (int i = 0; i < idCardWi.length; i++)
                      {
                        int current = Integer.parseInt(String.valueOf(charArray[i]));
                        int count = current * idCardWi[i];
                        sum += count;
                      }
                    char idCardLast = charArray[17];
                    int idCardMod = sum % 11;
                      return idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase());

                  } catch (Exception e)
                  {
                    e.printStackTrace();
                    return false;
                  }
              } else
              {
                return false;
              }
          }
        return matches;
      }

    /**
     * 校验集合是空的
     * @param dataList
     * @return true  表示集合为空
     */
      public static final boolean arrIsEmtry(List dataList){
          return null == dataList || dataList.isEmpty();
      }

    /**
     * 将空字符串转化为""
     */
    public static final String translateStrForNull(String str){
      if(null == str){
        return "";
      }
      return str;
    }
    /**
     * 格式化字符串为 utf-8""
     */
    public static final String translateStrForUtf8(String str){
      if(null == str){
        return "";
      }
      try {
//        String decode = URLDecoder.decode(str, "UTF-8");
// "古古怪怪123咕咕咕ggg "   转换之后  "+%E5%8F%A4%E5%8F%A4%E6%80%AA%E6%80%AA123%E5%92%95%E5%92%95%E5%92%95ggg"
        str=  URLEncoder.encode(str, "UTF-8");
      } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
      }
      return str;
    }
  }
